### get external defined data

LIBLORAGW_VERSION := 0.1.2
include library.cfg

### constant symbols

ARCH ?=arm
CROSS_COMPILE ?=arm-linux-gnueabihf-
CC := $(CROSS_COMPILE)gcc
AR := $(CROSS_COMPILE)ar

CFLAGS := -g -O2 -Wall -Wextra -fPIC -std=c99 -D_POSIX_C_SOURCE=200112L -D_BSD_SOURCE -Iinc -I.

OBJDIR = obj
LIBDIR = lib
INCLUDES = $(wildcard inc/*.h)

### linking options

LIBS := -lmd134x -lrt -lm -L./$(LIBDIR)

### general build targets

all: dirs libmd134x.a libmd134x.so

clean:
	rm -f $(LIBDIR)/libmd134x.a
	rm -f $(LIBDIR)/libmd134x.so    
	rm -f $(BINDIR)/test_loragw_*
	rm -f $(OBJDIR)/*.o
	rm -f inc/config.h
	rm -rf $(OBJDIR)
	rm -rf $(LIBDIR)

### transpose library.cfg into a C header file : config.h

inc/config.h: library.cfg
	@echo "*** Checking libloragw library configuration ***"
	@rm -f $@
    # File initialization
	@echo "#ifndef _LORAGW_CONFIGURATION_H" >> $@
	@echo "#define _LORAGW_CONFIGURATION_H" >> $@
    # Release version
	@echo "Release version   : $(LIBLORAGW_VERSION)"
	@echo -n " #define LIBLORAGW_VERSION  " >> $@
	@echo -n " \"" >> $@
	@echo -n "$(LIBLORAGW_VERSION)" >> $@
	@echo "\"" >> $@
    # Debug options
	@echo "	#define DEBUG_AUX	$(DEBUG_AUX)" >> $@
	@echo "	#define DEBUG_COM	$(DEBUG_COM)" >> $@
	@echo "	#define DEBUG_REG	$(DEBUG_REG)" >> $@
	@echo "	#define DEBUG_MCU	$(DEBUG_MCU)" >> $@
	@echo "	#define DEBUG_GPS	$(DEBUG_GPS)" >> $@
	@echo "	#define DEBUG_HAL	$(DEBUG_HAL)" >> $@
    # end of file
	@echo "#endif" >> $@
	@echo "*** Configuration seems ok ***"

### library module target
dirs:
	@mkdir -p $(OBJDIR)
	@mkdir -p $(LIBDIR)

$(OBJDIR)/%.o: src/%.c $(INCLUDES) inc/config.h
	$(CC) -c $(CFLAGS) $< -o $@

$(OBJDIR)/loragw_com_linux.o: src/loragw_com_linux.c $(INCLUDES) inc/config.h | $(OBJDIR)
	$(CC) -c $(CFLAGS) $< -o $@

$(OBJDIR)/loragw_com.o: src/loragw_com.c $(INCLUDES) inc/config.h | $(OBJDIR)
	$(CC) -c $(CFLAGS) $< -o $@

$(OBJDIR)/loragw_mcu.o: src/loragw_mcu.c $(INCLUDES) inc/config.h | $(OBJDIR)
	$(CC) -c $(CFLAGS) $< -o $@

$(OBJDIR)/loragw_hal.o: src/loragw_hal.c $(INCLUDES) src/arb_fw.var src/agc_fw.var src/cal_fw.var inc/config.h | $(OBJDIR)
	$(CC) -c $(CFLAGS) $< -o $@

### static library

libmd134x.a: $(OBJDIR)/loragw_hal.o $(OBJDIR)/loragw_mcu.o $(OBJDIR)/loragw_reg.o $(OBJDIR)/loragw_com.o $(OBJDIR)/loragw_com_linux.o $(OBJDIR)/loragw_aux.o $(OBJDIR)/loragw_radio.o
	$(AR) rcs $(LIBDIR)/$@ $^

### dynamic library
libmd134x.so: $(OBJDIR)/loragw_hal.o $(OBJDIR)/loragw_mcu.o $(OBJDIR)/loragw_reg.o $(OBJDIR)/loragw_com.o $(OBJDIR)/loragw_com_linux.o $(OBJDIR)/loragw_aux.o $(OBJDIR)/loragw_radio.o
	$(CC) -shared -fPIC -o $(LIBDIR)/$@ $^


### test programs

### EOF
